package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysNoticeBrowseEntity;
import com.start.admin.entity.StartSysNoticeEntity;
import com.start.admin.service.StartSysNoticeBrowseService;
import com.start.admin.service.StartSysNoticeService;
import com.start.admin.vo.StartUserNoticeVo;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.security.StartTokenTools;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 系统通知公告
 *
 * @author gxl
 * @email ${email}
 * @date 2021-02-22 17:24:48
 */
@RestController
@RequestMapping("/start/sys/sysNotice")
@RegisterMenu(value = "通知公告", systemCode = "sys:SysNotice", component = "views/sys/sysNotice/index", path = "/sysNotice/index")
public class StartSysNoticeController {
    private static final String LOG_TITLE = "通知公告";

    @Autowired
    private StartSysNoticeService startSysNoticeService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("sys/sysNotice/list")
    public Result<?> list(Page<StartSysNoticeEntity> page, StartSysNoticeEntity entity) {
        QueryWrapper<StartSysNoticeEntity> queryWrapper = DataQueryConstructor.createEntity(entity);
        return Result.ok(startSysNoticeService.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('sys:sysNotice:info')")
    @RegisterPermission(value = "sys:sysNotice:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(startSysNoticeService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('sys:sysNotice:add')")
    @RegisterPermission(value = "sys:sysNotice:add", name = "添加")
    public Result<?> save(@RequestBody StartSysNoticeEntity sysNotice) {
        startSysNoticeService.save(sysNotice);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('sys:sysNotice:edit')")
    @RegisterPermission(value = "sys:sysNotice:edit", name = "修改")
    public Result<?> update(@RequestBody StartSysNoticeEntity sysNotice) {
        startSysNoticeService.updateById(sysNotice);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('sys:sysNotice:delete')")
    @RegisterPermission(value = "sys:sysNotice:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        startSysNoticeService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/getNoticesList")
    public Result<?> getNoticesList(Page<StartUserNoticeVo> page, StartUserNoticeVo entity) {
        entity.setUserId(StartTokenTools.getCurrentUser().getUserId());
        return Result.ok(startSysNoticeService.pageUserNotice(page, entity));
    }

    @Autowired
    private StartSysNoticeBrowseService noticeBrowseService;

    @GetMapping("/getNoticeById")
    public Result<?> getNoticeById(String id) {
        StartSysNoticeEntity noticeEntity = startSysNoticeService.getById(id);
        if (noticeEntity == null) return Result.fail("该信息不存在");
        String userId = StartTokenTools.getCurrentUser().getUserId();
        StartSysNoticeBrowseEntity startSysNoticeBrowseEntity = noticeBrowseService.getByUserIdAndNoticeId(userId, noticeEntity.getId());
        if (startSysNoticeBrowseEntity == null) {
            startSysNoticeBrowseEntity = new StartSysNoticeBrowseEntity();
            startSysNoticeBrowseEntity.setNoticeId(id);
            startSysNoticeBrowseEntity.setBrowseTime(LocalDateTime.now());
            startSysNoticeBrowseEntity.setCreateBy(userId);
            startSysNoticeBrowseEntity.setBrowseUserId(userId);
        }
        startSysNoticeBrowseEntity.setUpdateBy(userId);
        noticeBrowseService.saveOrUpdate(startSysNoticeBrowseEntity);
        return Result.ok(noticeEntity);
    }
}
